﻿using UnityEngine;
using UnityEngine.UI;

namespace Core
{
    public enum EUILayer
    {
        Normal,
        Pop
    }

    public class UILayer : Core.Component, IAwake<EUILayer, GameObject>
    {
        public EUILayer Layer { get; private set; }
        public GameObject GameObject { get; private set; }
        public Transform Transform { get; private set; }
        public Canvas Canvas { get; private set; }
        public CanvasScaler Scaler { get; private set; }

        public void Awake(EUILayer layer, GameObject t)
        {
            Layer = layer;
            GameObject = t;
            Transform = t.transform;
            Canvas = t.GetComponent<Canvas>();
            Scaler = t.GetComponent<CanvasScaler>();
        }

        public void SetSceneOrientation(ESceneOrientation orientation)
        {
            var referenceResolution = new Vector2(720, 1280);
            var matchWidthOrHeight = 0;

            if (orientation == ESceneOrientation.LandScape)
            {
                referenceResolution = new Vector2(1280, 720);
                matchWidthOrHeight = 1;

                float screenWidth = Screen.width;
                float screenHeight = Screen.height;
                if (screenWidth / screenHeight < 1280.0f / 720)
                {
                    matchWidthOrHeight = 0;
                }
            }

            Scaler.referenceResolution = referenceResolution;
            Scaler.matchWidthOrHeight = matchWidthOrHeight;
        }
    }
}